package com.xing.controller;

import com.xing.DTO.MenuSaveDTO;
import com.xing.DTO.MenuUpdateDTO;
import com.xing.Utils.Result;
import com.xing.entity.SysMenuEntity;
import com.xing.service.SysMenuService;
import com.xing.utils.PageData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "权限服务")
@RestController
@RequestMapping(value = "menu")
public class SysMenuController {

    @Autowired
    private SysMenuService menuService;

    @Operation(summary = "查询所有权限信息")
    @GetMapping(value = "listAll")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result listAll() {
        List<SysMenuEntity> menuEntityList = menuService.listAll();
        return new Result().ok(menuEntityList);
    }


    @Operation(summary = "分页查询权限信息")
    @GetMapping(value = "listByPage")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result listByPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        PageData<SysMenuEntity> menuEntityPage = menuService.listByPage(pageNum, pageSize);
        return new Result().ok(menuEntityPage);
    }


    @Operation(summary = "新增权限信息")
    @PostMapping(value = "saveMenu")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result saveMenu(@RequestBody MenuSaveDTO menuSaveDTO) {
        return menuService.saveMenu(menuSaveDTO);
    }


    @Operation(summary = "修改权限信息")
    @PutMapping(value = "updateMenu")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result updateById(@RequestBody MenuUpdateDTO menuUpdateDTO) {
        return menuService.updateById(menuUpdateDTO);
    }


    @Operation(summary = "删除权限信息")
    @DeleteMapping(value = "deleteById")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result deleteById(@RequestParam Long id) {
        return menuService.deleteById(id);
    }


}
